<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>title</title>
    <link rel="stylesheet" href="/statics/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="/statics/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <script src="/statics/jquery.min.js"></script>
    <script src="/statics/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <style>
        .panel-body {
            height: 400px;
            overflow: auto;
            overflow-x: hidden;
        }

        .label {
            padding: 0.3em .5em .5em;
            line-height: 1.5;
            display: inline-block;
            white-space: inherit;
            text-align: left;
            word-break: break-all;
        }

        html, body {
            font: 12px/1 "Microsoft Yahei", "微软雅黑", "黑体", "SimHei", Arial, sans-serif;
            color: #2E2E2E;
            height: 100%;
        }

        .panel-body::-webkit-scrollbar {
            height: 8px;
            width: 8px;
            background: rgba(152, 152, 152, 0.3);
            border-radius: 5px;
        }

        .panel-body::-webkit-scrollbar-button {
            display: none;
        }

        .panel-body::-webkit-scrollbar-track {
            background-color: #ffffff;
        }

        .panel-body::-webkit-scrollbar-track-piece {
            background: #ffffff;
        }

        .panel-body::-webkit-scrollbar-thumb {
            width: 8px;
            min-height: 15px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 5px;
        }

        .panel-body::-webkit-scrollbar-thumb:hover {
            background: rgba(0, 0, 0, 0.6);
        }

        .panel-body::-webkit-scrollbar-thumb:active {
            background: rgba(0, 0, 0, 0.8);
        }

        p {
            padding-bottom: 10px;
        }

        .alert {
            padding: 9px;
            text-align: center;
        }

        .container {
            max-width: 670px;
        }
    </style>
</head>
<body>
<div class="container">

    <div class="page-header">
        <h1>
            websocket
            <small>websocket for php</small>
        </h1>
    </div>
    <div class="panel panel-default">
        <div class="panel-body">

        </div>
    </div>
    <div class="form-group has-success has-feedback">
        <label class="control-label" for="message">请文明发言,勿刷屏,按回车键发送消息</label>
        <input type="text" class="form-control" id="message" placeholder="输入你想对大家说的话">
    </div>
</div>

<script>
    var socket = io.connect(document.URL+'?domain=' + document.domain, {a: 3});
    socket.on('connect', function () {
        console.log('connect')
        log('连接成功!');
        var UserID = 1;//prompt("请输入您的ID","");
        socket.emit('login', {userId: UserID});
    });
    socket.on('disconnect', function () {
        console.log('disconnect')
        log('断开连接!', 'danger')
    });
    socket.on('test event', function (data) {
        console.log(data)
        log('test event' + data)
    });
    socket.on('message', function (message) {
        addMessage(message, 'left');
    });


    $(function () {
        $('#message').keypress(function (e) {
            if (e.keyCode == 13 && this.value) {
                addMessage($(this).val(), 'right');
                $.post('/sender/12', {data: 'asf', event: 'asf'}, function () {
                    console.log(arguments)
                });
                /*if(!socket.connected){
                 log('没有连接服务器!', 'danger');
                 }else{
                 socket.send($(this).val())
                 }*/
                $(this).val('');
            }
        });
    });


    // 将消息上屏
    function addMessage(msg, x, y) {
        if (!y) {
            y = "success";
        }

        $('<p class="text-' + x + '">\
                   <span class="label label-' + y + '">' + msg + '</span>\
              </p>').appendTo('.panel-body');

        $log = $('.panel-body');
        $log[0].scrollTop = $log[0].scrollHeight - $log[0].clientHeight;
    }
    function log(msg, x) {
        if (!x) {
            x = "success";
        }
        $('<div class="alert alert-' + x + '" role="alert">' + msg + '</div>').appendTo('.panel-body')
        $log = $('.panel-body');
        $log[0].scrollTop = $log[0].scrollHeight - $log[0].clientHeight;
    }


</script>
</body>
</html>